<?php

include_once 'connection.php';
include_once 'convert.php';
include_once '../info_struct.php';

/*
 * Usage:
 * get_by_ext_id(id_kind, ref);
 * Parameter:
 * id_kind - Kind of the id (doi, zmath or arxiv)
 * ref - infos object containing the id to search for
 * Return:
 * infos object containing the given id
 */
function ldb_get_by_ext_id($id_kind, $ref) {
    $ret = null;
    /* Get reference by its doi */
    if ($id_kind == "doi") {
        /* Generate SQL-query for retrieving references with the given external id */
        $sql = infos_2_sql_search_doi($ref);
        /* Run query on the database */
        $qres = ldb_query_complete($sql);
        
        if(!$qres) {
            return false;
        }
      /* Get reference by its zmath-id */  
    } elseif ($id_kind == "zmath") {
        $sql = infos_2_sql_search_zmath($ref);
        $qres = ldb_query_complete($sql);
        
        if(!$qres) {
            return false;
        }
        
      /* Get reference by its arxiv-id */
    } elseif ($id_kind == "arxiv") {
        $sql = infos_2_sql_search_arxiv($ref);
        $qres = ldb_query_complete($sql);
        
        if(!$qres) {
            return false;
        }
        
    }
    /* Generate list of infos objects from the SQL-result */
    $ret = sql_result_2_infos($qres);
    /* If list is not empty return list else false */
    if (count($ret) >= 1) {
        return $ret;
    } else {
        return false;
    }
}

/*
 * Usage:
 * ldb_get_from_to(from, to);
 * Parameter:
 * from - Id of first reference that is to be contained in the returned list
 * to - Id of last reference that is to be contained in the returned list
 * Return:
 * List of infos objects containing references with ids from <= id <= to
 */
function ldb_get_from_to($from, $to) {
    /* Generate SQL-query for retrieving the list from the database */
    $sql = query_list_from_to($from, $to);
    /* Run query on the database */
    $res = ldb_query_complete($sql);
    if (!$res) {
        return false;
    }
    /* Generate list of infos object from SQL-result */
    $ret = sql_result_2_infos($res);

    return $ret;
}

function ldb_get_list() {
    $sql = query_list_full();
    
    $res = ldb_query_complete($sql);
    if (!$res) {
        return false;
    }
    
    $ret = sql_result_2_infos($res);
    
    return $ret;
}

/*
 * Usage:
 * ldb_get_by_id(id);
 * Parameter:
 * id - ID of an reference in the local database
 * Return:
 * infos object containing the information of the searched reference
 * 
 * IMPORTANT: Intended to be only used for the tooltip-function
 */
function ldb_get_by_id($id) {
    /* Generate query for retrieving reference with given id */
    $sql = id_2_sql_get_by_id($id);
    /* Run query on the database */
    $res = ldb_query_complete($sql);
    if (!$res) {
        return false;
    }
    /* Generate list of infos object from SQL-result
     * (in this case the list will contain only one item)
     */
    $ref = sql_result_2_infos($res);
    if (count($ref) === 0) {
        return false;
    }
    /* Return the first (and only) element of the list */
    return $ref[0];
}





